<html>
<head><meta charset="utf-8"><title>String::from(&amp;str) wonky codegen · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/String.3A.3Afrom(.26str).20wonky.20codegen.html">String::from(&amp;str) wonky codegen</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="216164455"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/String%3A%3Afrom%28%26str%29%20wonky%20codegen/near/216164455" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/String.3A.3Afrom(.26str).20wonky.20codegen.html#216164455">(Nov 10 2020 at 01:41)</a>:</h4>
<p>Someone should fix this: <a href="https://rust.godbolt.org/z/e4ePav">https://rust.godbolt.org/z/e4ePav</a> — it seems like it goes through a trillion calls and boils down to Vec::with_capacity + Vec::extend_from_slice, but the 2nd seems not to know the capacity is fine (hence the realloc).</p>
<p>Note that <code>Box::&lt;str&gt;::from(s).into_string()</code> produces acceptable code (well, slightly less than ideal control flow), but <code>into_string</code> is needed as <code>String::from(Box&lt;str&gt;)</code> seems not to be inlined... Most other equivalents that dont go through <code>Box&lt;str&gt;</code>/<code>Box&lt;[u8]&gt;</code> seem to have the same issue. — the rest mainly go though str::to_owned.</p>



<a name="216175760"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/String%3A%3Afrom%28%26str%29%20wonky%20codegen/near/216175760" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/String.3A.3Afrom(.26str).20wonky.20codegen.html#216175760">(Nov 10 2020 at 06:18)</a>:</h4>
<p>yeah looks like that should be improved. can you open a github issue?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>